#ifndef _DEVICE_H
#define _DEVICE_H

// 设备驱动包含
#include "device_conf.h"
#include "dev_led.h"
#include "dev_oled.h"
#include "dev_key.h"
#include "dev_buzzer.h"
#include "dev_mpu6050.h"
#include "dev_mpu9250.h"
#include "dev_pwm.h"
#include "dev_motor.h"
#include "dev_usart.h"
#include "dev_timer_interrupt.h"

#define LED         (1 << 0)
#define OLED        (1 << 1)
#define KEY         (1 << 2)
#define BUZZER      (1 << 3)
#define MPU6050     (1 << 4)
#define PWM         (1 << 5)
#define USART       (1 << 6)
#define TIMER       (1 << 7)
#define MOTOR       (1 << 8)
#define SERVO       (1 << 9)

// LED
typedef struct _led_t
{
    void (*init)(void);     // LED初始化
    void (*blink)(uint16 cycle,uint16 time); // LED闪烁
} led_t;

// OLED
typedef struct _oled_t
{
    void(*init)(void); // OLED初始化
    void(*clear)(void); // OLED清屏
    void(*show_ch)(uint8 x, uint8 y, char ch); // 显示一个6x8标准ASCII字符
    void(*show_str)(uint8 x, uint8 y, char *ch); // 写入一组6x8标准ASCII字符串
    void(*show_value_int)(uint8 x, uint8 y, int32 data); // 将一个int型数转换成5位数进行显示	
    void(*show_value_float)(uint8 x, uint8 y, float data, uint8 num); //将一个float型数转换成5位数带小数和符号的数据进行显示
    void(*show_img)(uint8 *pucTable, uint32 usRowNum, uint32 usColumnNum); // 显示图像
    void(*draw_bmp)(uint8 x0, uint8 y0, uint8 x1, uint8 y1, uint8 BMP[]); // 显示BMP图片
} oled_t;

// KEY
typedef struct _key_t
{
    void(*init)(void); // 按键初始化
	void(*tim_check)(uint32 tim_freq, f32 time); // 按键长按检测（此函数用于定时器中断中）
    uint8 (*scan)(uint16 time); // 按键扫描：返回1byte数据，每个bit代表一个按键位，time: 长按触发时间
	uint8 (*tim_scan)(uint32 tim_freq, f32 time, uint8 key_value_check); // 按键长按扫描（此函数用于定时器中断中）
} key_t;

// BUZZER
typedef struct _buzzer_t
{
    void (*init)(void); // BEEP初始化
    void (*beep)(uint16 cycle,uint16 time); // 蜂鸣器报警
} buzzer_t;

// MPU6050
typedef struct _mpu6050_t
{
    mpu6050_data_t *data;
    void (*init)(void); // MPU6050设备初始化
    void (*get_raw_data)(void);
} mpu6050_t;

// PWM
typedef struct _pwm_t
{
    void (*init)(uint16 arr, uint16 psc); // PWM初始化
    void (*c_output_8ch)(uint16 compare1, uint16 compare2, uint16 compare3, uint16 compare4); // 8路PWM互补输出
} pwm_t;

// SERVO
typedef struct _servo_t
{
    
} servo_t;

// MOTOR
typedef struct _motor_t
{
    void (*init)(uint16 period, uint16 frequency); // 电机PWM初始化函数；period: 计数周期；frequency：PWM频率
    void (*output_4ch)(uint16 compare1, uint16 compare2, uint16 compare3, uint16 compare4); // 电机4路PWM输出
} motor_t;

// USART
typedef struct _usart_t
{
    int8 *receive_buffer; // 指向USART接收数据的内存空间，此空间占4个byte，首尾字节用于校验，中间两字节为有效数据
    void (*init)(uint32 bound);
    void (*send_message)(int8 cmd, int8 data); // USART发送数据
} usart_t;

// TIMER
typedef struct _timer_t
{
    void (*init)(uint16 frequency); // 定时器中断初始化；frequency：定时器中断频率
} timer_t;

// 初始化参数
typedef struct _para_t
{
    uint32 timer_frequency;      // 定时器工作频率
    uint32 pwm_period;           // PWM计数频率
    uint32 pwm_frequency;        // PWM频率
    uint32 motor_pwm_period;     // 电机PWM计数周期
    uint32 motor_pwm_frequency;  // 电机PWM工作频率
    uint32 servo_pwm_period;     // 电机PWM计数周期
    uint32 servo_pwm_frequency;  // 舵机PWM工作频率
    uint32 usart_baud;           // 串口波特率
} para_t;

// 设备驱动结构体
typedef struct _device_t
{
    led_t       led;
    oled_t      oled;
    key_t       key;
    buzzer_t    buzzer;
    mpu6050_t   mpu6050;
    pwm_t       pwm;
    motor_t     motor;
    usart_t     usart;
    timer_t     timer;
    
    para_t      para;
    
    void(*init)(uint32 device_select);
} device_t;

// 驱动库初始化
void device_init(device_t *device, uint32 device_select);

#endif
